
global username = c(username)
global dropbox "/Users/${username}/Dropbox"
global master_file_path "${dropbox}/Engel_GFT/replication_files"


set more off

global output "$master_file_path/data/intermediate_data"


clear all
set more off
set matsize 11000

cd "$master_file_path\data\intermediate_data/hh_shares"


foreach ender in V1_D {

use "../R43R55/expenditures_R43R55_i_groups`ender'.dta", clear

drop g_groupsV1 g_groupsV2 g_groupsV3
drop if i_groupsV1=="Other fuel"

rename i_groupsV1 i_products

merge m:1 i_products using  "$master_file_path/data/intermediate_data/Alternative_Gs/G_Groupings_108.dta", keepusing(G_1)
drop _merge


gen xsector=1 if sector=="Rural"
replace xsector=2 if sector=="Urban"
drop sector
rename xsector sector

drop if sector!=1

drop if wt==0  | wt==.
drop if hhid==.
drop if state43==. | district43==.
drop if sector==.

gegen tag=tag(hhid)
keep if tag==1
drop tag
egen market_year_id=group(state43 district43 sector round)
egen market_id=group(state43 district43 sector)
gen logexp_cap=ln(mpce)
winsor2 logexp_cap, suffix(w) cuts(0.1 99.9) by(round sector)
*Integer weights for lpoly:
gen int_wt=int(wt)
*count housheolds in cell
egen count_hh=count(hhid),by(market_year_id)
egen total_wt=total(wt),by(market_year_id)
keep round sector hhid wt state43 district43 market_year_id market_id logexp_cap logexp_capw int_wt count_hh total_wt hhsize flag*
save hh_43_55`ender'_G108.dta, replace
}


















cd "$master_file_path/data/intermediate_data/R43R55"

forvalues G=1(1)108 {

foreach ender in V1_D {

use expenditures_R43R55_i_groups`ender'.dta , clear

drop g_groupsV1 g_groupsV2 g_groupsV3
drop if i_groupsV1=="Other fuel"

rename i_groupsV1 i_products

merge m:1 i_products using "$master_file_path/data/intermediate_data/G_Groupings_108.dta", keepusing(G_`G')
drop _merge


gen xsector=1 if sector=="Rural"
replace xsector=2 if sector=="Urban"
drop sector
rename xsector sector

drop if sector!=1

drop if wt==0  | wt==.
drop if hhid==.
drop if state43==. | district43==.
drop if sector==.


keep hhid i_products G_`G' expenditure



encode i_products, generate(item_id)
drop i_products


fillin item_id hhid

egen m=mean(G_`G'), by(item_id)
replace G_`G'=m
drop m



preserve
gegen tag=tag(item_id)
keep if tag==1
keep item_id G_`G'
generate item_no=item_id
save item_codes`ender'_G108_`G'.dta, replace
save item_codes`ender'M_G108_`G'.dta, replace
restore


replace expenditure=0 if expenditure==.

gegen sum_exp_G_`G'=total(expenditure),by(hhid G_`G')
gen rshare_=expenditure/sum_exp_G_`G'

keep item_id hhid rshare_

reshape wide rshare_ , i(hhid) j(item_id)


merge m:1 hhid using "$master_file_path\data\intermediate_data/hh_shares/hh_43_55`ender'_G108.dta", nogen


keep round sector hhid wt state43 district43 market_year_id market_id logexp_cap logexp_capw int_wt count_hh total_wt hhsize flag* rshare_*

cap drop if flag_mpce==1 // these have extreme mpces
compress
save "$master_file_path/data/intermediate_data/hh_shares/hh_shares_43_55`ender'M_G108_`G'_rural.dta", replace

}
}

    set more off


set more off
set matsize 11000

cd "$master_file_path/data/intermediate_data/R43R55"



foreach ender in V1_D {

use expenditures_R43R55_i_groups`ender'.dta , clear

drop g_groupsV1 g_groupsV2 g_groupsV3
drop if i_groupsV1=="Other fuel"

rename i_groupsV1 i_products

merge m:1 i_products using "$master_file_path/data/intermediate_data/Alternative_Gs/G_Groupings_108.dta", keepusing(G_name G_15)

drop _merge


collapse (sum) expenditure, by(sector state43 hhid hhsize wt mpce flag_mpce Household_size Religion Caste kids HH_education district43 round G_name G_15)

rename G_name i_products

gen xsector=1 if sector=="Rural"
replace xsector=2 if sector=="Urban"
drop sector
rename xsector sector

drop if sector!=1

drop if wt==0  | wt==.
drop if hhid==.
drop if state43==. | district43==.
drop if sector==.


keep hhid i_products G_15 expenditure



encode i_products, generate(item_id)
drop i_products


fillin item_id hhid

egen m=mean(G_15), by(item_id)
replace G_15=m
drop m



preserve
gegen tag=tag(item_id)
keep if tag==1
keep item_id G_15
generate item_no=item_id
save item_codes`ender'_G108_15_8i.dta, replace
save item_codes`ender'M_G108_15_8i.dta, replace
restore


replace expenditure=0 if expenditure==.

gegen sum_exp_G_15=total(expenditure),by(hhid G_15)
gen rshare_=expenditure/sum_exp_G_15

keep item_id hhid rshare_

reshape wide rshare_ , i(hhid) j(item_id)


merge m:1 hhid using "$master_file_path\data\intermediate_data/hh_shares/hh_43_55`ender'_G108.dta", nogen

keep round sector hhid wt state43 district43 market_year_id market_id logexp_cap logexp_capw int_wt count_hh total_wt hhsize flag* rshare_*


cap drop if flag_mpce==1 // these have extreme mpces
compress
save "$master_file_path/data/intermediate_data/hh_shares/hh_shares_43_55`ender'M_G108_15_rural_8i.dta", replace

}



*****NOW WITH 136 PRODUCT GROUPS************************************


cd "$master_file_path/data/intermediate_data/R43R55"


    set more off


*clear all
set more off
set matsize 11000



foreach ender in V1_D {

use expenditures_item_level_R43R55_i_groupsV1, clear

drop if hhid==.

drop if flag_deaton_drops==1

drop g_groupsV1 g_groupsV2 g_groupsV3
drop if i_groupsV1=="Other fuel"

rename i_groupsV1 i_products

merge m:1 i_products using "$master_file_path/data/intermediate_data/G_Groupings_108.dta", keepusing(G_name G_15)
drop _merge


collapse (sum) expenditure, by(sector state43 hhid hhsize wt mpce flag_mpce Household_size Religion Caste kids HH_education district43 round concorded_itemlabel2 G_15)

rename concorded_itemlabel2 i_products

gen xsector=1 if sector=="Rural"
replace xsector=2 if sector=="Urban"
drop sector
rename xsector sector

drop if sector!=1

drop if wt==0  | wt==.
drop if hhid==.
drop if state43==. | district43==.
drop if sector==.


keep hhid i_products G_15 expenditure



encode i_products, generate(item_id)
drop i_products


fillin item_id hhid

egen m=mean(G_15), by(item_id)
replace G_15=m
drop m



preserve
gegen tag=tag(item_id)
keep if tag==1
keep item_id G_15
generate item_no=item_id
save item_codes`ender'_G108_15_136i.dta, replace
save item_codes`ender'M_G108_15_136i.dta, replace
restore


replace expenditure=0 if expenditure==.

gegen sum_exp_G_15=total(expenditure),by(hhid G_15)
gen rshare_=expenditure/sum_exp_G_15

keep item_id hhid rshare_

reshape wide rshare_ , i(hhid) j(item_id)


merge m:1 hhid using "$master_file_path\data\intermediate_data/hh_shares/hh_43_55`ender'_G108.dta", nogen




keep round sector hhid wt state43 district43 market_year_id market_id logexp_cap logexp_capw int_wt count_hh total_wt hhsize flag* rshare_*

cap drop if flag_mpce==1 // these have extreme mpces
compress
save "$master_file_path/data/intermediate_data/hh_shares/hh_shares_43_55`ender'M_G108_15_rural_136i.dta", replace

}
